/* 
 * Log Structure Layer
 * 
 * logrw.h
 *
 * Interface to the LOGRW storage layer which offers the abstraction
 * of a sequence of fixed size registers.
 * 
 */

#ifndef _LOGRW_H_
#define _LOGRW_H_

#define CLEAN_PRIO 0

/*
 * logrw_struct_t: the storage abstraction of an array of registers
 * the implementation is hidden, and should be implemented by the students
 */
typedef struct logrw_struct_ logrw_struct_t;


/*
 * logrw_init: create a logrw instance
 * should allocate space for all registers
 * should allocate space for block arrays
 * - num_reg: number of registers
 * - array_dim: size of each of the two arrays of blocks
 * - bloco_dim: size of each block/register
 *   returns: pointer to the logrw instance
 */
logrw_struct_t* logrw_init(unsigned int num_reg, unsigned int array_dim, unsigned int bloco_dim);


/*
 * logrw_free: free the logrw
 * - log - the logrw instance
 */
void logrw_free(logrw_struct_t* log);


/*
 * logrw_num_registers: gets the number of registers
 * - log - the logrw instance
 *   returns: number of the num_reg parameter that was provided when
 *   the logrw was first initialized; -1 if error
 */
int logrw_num_registers(logrw_struct_t* log);


/*
 * logrw_read: reads a single register, reading the whole block with the 
 * most up-to-date content for that register
 * - log - the logrw instance
 * - register_no: the number of the register to read
 * - block: the buffer were to copy the block containing that register content [out]
 *   returns: 0 if sucessful, -1 if not
 */
int logrw_read(logrw_struct_t* log, unsigned register_no, char* block);


/*
 * logrw_write: writes a register into the next free array block, writing to the whole block
 * - log: the logrw instance
 * - register_no: the number of the register to write
 * - block: the buffer with the data to write
 *   returns: 0 if sucessful, -1 if not
 */
int logrw_write(logrw_struct_t* log, unsigned register_no, char* block);


/*
 * logrw_dump: prints general debugging information, including:
 * - which array is active ("first array"/"second array")
 * - index for next free block in the active array
 * - for each register, the index and array ("active array"/"inactive array") 
 * of the block that currently holds the most up-to-date contents of the register
 * Input:
 * - log - the logrw instance
 */
void logrw_dump(logrw_struct_t* log);


/*
 * logrw_dump_register: prints general information about a register 
 * If a valid register number is provided, the printed information must include:
 * - register number, 
 * - the index and array ("active array"/"inactive array") of the block 
 * that currently holds the most up-to-date contents of the register
 * - the first 8 bytes of the current contents of the register, in 
 * hexadecimal representation.
 * Input:
 * - i - the number of the register
 * - log - the logrw instance
 */
void logrw_dump_register(unsigned int i, logrw_struct_t* log); 


/*
 * logrw_dump_all_registers: prints general information about all registers
 * For each register, the printed information should be the same as printed
 * by logrw_dump_register.
 * - log - the logrw instance
 */
void logrw_dump_all_registers(logrw_struct_t* log);


#endif
